package com.kfc.platform.transaction.dao.impl;

import java.math.BigDecimal;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.hibernate.Session;

import com.kfc.platform.framework.jpa.BaseDaoImpl;
import com.kfc.platform.transaction.dao.TransactionDao;
import com.kfc.platform.transaction.domain.TradeList;
import com.kfc.platform.user.dao.UserDao;
import com.kfc.platform.user.domain.User;

public class TransactionDaoImpl extends BaseDaoImpl<TradeList> implements TransactionDao {
	@PersistenceContext
	protected EntityManager entityManager;
	
	public TradeList insertgetId(TradeList tradeList){
		Session s = this.getSession();
		s.save(tradeList);
		return tradeList;
	}
	

	@Override
	public void updatebyId(Object id,BigDecimal num) {
		// TODO Auto-generated method stub
		Session s = this.getSession();
		String status = "未成交";
		List<BigDecimal> ls = s.createQuery("select t.amount from TradeList " +
				"t where t.listnum="+id).list();
		
		BigDecimal tempnum = ls.get(0);
		if(num.compareTo(tempnum) == 0)
			status = "已成交";
		else if(num.compareTo(tempnum) == -1)
			status = "部分成交";
		s.createQuery("update TradeList t set t.amount="+num.subtract(tempnum)+",t.status='"+status+"' where t.listnum="+id).executeUpdate();
		
	}


	@Override
	public int savegetid(TradeList tradelist) {
		// TODO Auto-generated method stub
		getSession().save(tradelist);
		return tradelist.getListnum();
	}
	protected Session getSession() {
//      return (Session) entityManager.getDelegate();
      return entityManager.unwrap(Session.class);
  }


	@Override
	public List<TradeList> findlistbytype(String type) {
		// TODO Auto-generated method stub
		Session s = this.getSession();
		List<TradeList> ls = s.createQuery("from TradeList t where t.type='"+type+"'").list();
		return ls;
	}
}
